import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


export default new Vuex.Store({
  state:{
    cartData:[],
    categoryListData:null
  },
  mutations: {
    addcartGoods(state,data){
      let cart = state.cartData;
      if(cart.length < 1){
        state.cartData.push(data);
      }else{
        let exists = [];
        state.cartData.forEach((item,key) => {
          if(item.goodsId === data.goodsId && item.collection_id === data.collection_id){
            exists.push(true)
          }else{
            exists.push(false);
          }
        });
        if(exists.indexOf(true) > -1){
          console.log(state.cartData[exists.indexOf(true)])
          state.cartData[exists.indexOf(true)].number += data.number;
        }else{
          state.cartData.push(data);
        }
      }
    }
    
  },
  actions: {
  }
})